package myhttp

import (
	"fmt"
	"github.com/parnurzeal/gorequest"
	"net/url"
	"strings"
)

func Get(reqUrl string) ([]byte, error) {

	request := gorequest.New()
	_, body, errs := request.Get(reqUrl).End()
	if errs != nil {
		return nil, errs[0]
	}

	return []byte(body), nil
}
func Post(reqUrl string, requestBody map[string]string) ([]byte, error) {
	queryString := concatenateURLParams(requestBody)
	request := gorequest.New()
	_, body, errs := request.Post(reqUrl).
		Send(queryString).
		End()
	if errs != nil {
		return nil, errs[0]
	}

	return []byte(body), nil
}
func concatenateURLParams(params map[string]string) string {
	var queryString []string

	for k, v := range params {
		// URL编码键和值，确保特殊字符被正确处理
		encodedKey := url.QueryEscape(k)
		encodedValue := url.QueryEscape(v)
		// 拼接成 "key=value" 形式的字符串并添加到结果列表中
		queryString = append(queryString, fmt.Sprintf("%s=%s", encodedKey, encodedValue))
	}

	// 使用 "&" 连接所有参数对
	return strings.Join(queryString, "&")
}
